
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>hello</title>
    <!-- 导入vue包 -->
    <script src="./lib/vue2.6.10.js"></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="n1"> 
        <select v-model="opt">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input type="text" v-model="n2">
        <input type="button" value="=" @click="calc2">
        <input type="text" :value="res">
    </div>


    <script>
        // 创建一个vue实例
        var vm = new Vue({
            el:'#app', // 当前new的这个vue实例要控制的DOM
            data:{     // 所有的数据
                n1: 0,
                n2: 0,
                opt: '+',
                res: 0
            },
            methods: {
                calc(){
                    var res = 0;
                    switch (this.opt) {
                        case "+":
                            res = parseInt(this.n1) + parseInt(this.n2);
                            break;
                        case "-":
                            res = parseInt(this.n1) - parseInt(this.n2);
                            break;
                    
                        case "*":
                            res = parseInt(this.n1) * parseInt(this.n2);
                            break;
                        case "/":
                            res = parseInt(this.n1) / parseInt(this.n2);
                            break;
                    
                        default:
                            break;
                    }
                    this.res = res;
                },
                calc2(){
                    codeStr = "parseInt(this.n1) "+this.opt+"parseInt(this.n2)"
                    this.res = eval(codeStr);
                }
            },
        })

    </script>
</body>
</html>